package com.xplusplus.security;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/**
 * @Author: zhouweixin
 * @Description:
 * @Date: Created in 14:55 2018/9/4
 * @Modified By:
 */
@Configuration
public class MVCConfiguration extends WebMvcConfigurerAdapter {
    public static final String SESSION_KEY = "user";
    public static final String LOGIN_URL = "/login.html";

    // 设置主页
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("/login.html");

        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);

        super.addViewControllers(registry);
    }

    @Bean
    public SecurityInterceptor getSecurityInterceptor() {
        return new SecurityInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration interceptorRegistration = registry.addInterceptor(getSecurityInterceptor());

        // 登录不拦截
        interceptorRegistration.excludePathPatterns("/user/login**");
        interceptorRegistration.excludePathPatterns(LOGIN_URL);

        // 拦截所有
        interceptorRegistration.addPathPatterns("/**");
    }

    public class SecurityInterceptor extends HandlerInterceptorAdapter {
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

            // 生产环境和开发环境两点区别
            // 1.生产isClose为false; 开发环境为true
            // 2.开发环境要注释掉https

            // true 关闭安全过滤；false 打开安全过滤
            boolean isClose = false;
            if (isClose) {
                return true;
            }

            HttpSession session = request.getSession();
            if (session.getAttribute(SESSION_KEY) != null) {
                return true;
            }

            if ("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))) {
                //告诉ajax我是重定向
//                response.setHeader("REDIRECT", "REDIRECT");
                //告诉ajax我重定向的路径
                response.setHeader("CONTENTPATH", LOGIN_URL);
                response.setStatus(HttpServletResponse.SC_FORBIDDEN);
            } else {
                response.sendRedirect(LOGIN_URL);
            }

            return false;
        }
    }
}
